﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shapes
{
    // 定义一个接口，包含计算面积和判断形状是否合法的方法
    public interface IShape
    {
        double CalculateArea();
        bool IsValid();
    }

    // 抽象类Shape，实现IShape接口
    public abstract class Shape : IShape
    {
        public abstract double CalculateArea();
        public abstract bool IsValid();
    }

    // 长方形类
    public class Rectangle : Shape
    {
        public double Width { get; set; }
        public double Height { get; set; }

        public Rectangle(double width, double height)
        {
            Width = width;
            Height = height;
        }

        public override double CalculateArea()
        {
            return Width * Height;
        }

        public override bool IsValid()
        {
            return Width > 0 && Height > 0;
        }
    }

    // 正方形类
    public class Square : Shape
    {
        public double SideLength { get; set; }

        public Square(double sideLength)
        {
            SideLength = sideLength;
        }

        public override double CalculateArea()
        {
            return SideLength * SideLength;
        }

        public override bool IsValid()
        {
            return SideLength > 0;
        }
    }

    // 圆形类
    public class Circle : Shape
    {
        public double Radius { get; set; }

        public Circle(double radius)
        {
            Radius = radius;
        }

        public override double CalculateArea()
        {
            return Math.PI * Radius * Radius;
        }

        public override bool IsValid()
        {
            return Radius > 0;
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            Shape[] shapes = new Shape[10];
            double totalArea = 0;

            for (int i = 0; i < shapes.Length; i++)
            {
                int shapeType = random.Next(0, 3); // 随机生成0、1、2，分别代表长方形、正方形、圆形

                switch (shapeType)
                {
                    case 0:
                        shapes[i] = new Rectangle(random.NextDouble() * 10, random.NextDouble() * 10);
                        break;
                    case 1:
                        shapes[i] = new Square(random.NextDouble() * 10);
                        break;
                    case 2:
                        shapes[i] = new Circle(random.NextDouble() * 10);
                        break;
                }

                if (shapes[i].IsValid())
                {
                    totalArea += shapes[i].CalculateArea();
                }
                else
                {
                    Console.WriteLine("生成的形状不合法，跳过计算。");
                }
            }

            Console.WriteLine($"10个形状的总面积为: {totalArea}");
            Console.ReadKey();
        }
    }
}
